#include <QApplication>
#include <QFormLayout>
#include <QFileInfo>
#include "DocModels/aisdocdatasource.h"
#include "DocViews/aisplaintextview.h"
#include "DocViews/aispagedocview.h"
#include "aisdocview.h"

AISDocView::AISDocView(QWidget *parent) :
    QWidget(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
}

bool AISDocView::newFile(const QString &filePath)
{
    curFilePath = filePath;
    setWindowTitle(QFileInfo(curFilePath).fileName());

    curDocView = NULL;
    switch (getDocumentType())
    {
    case DT_TXT:
    {
        curDocView = new AISPlainTextView;
        break;
    }
    case DT_WORD:
    case DT_PPT:
    case DT_PDF:
    {
        curDocView = new AISPageDocView;
        break;
    }
    default:
        break;
    }

    if (curDocView)
    {
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(curDocView);
        layout->setContentsMargins(QMargins());
        setLayout(layout);
        return true;
    }

    return false;
}

bool AISDocView::loadFile(const QString &filePath)
{
    dataSource = new AISDocDataSource;
    if (NULL != dataSource && dataSource->loadFile(filePath))
    {
        curFilePath = filePath;
        setWindowTitle(QFileInfo(curFilePath).fileName());

        curDocView = NULL;
        switch (getDocumentType())
        {
        case DT_TXT:
        {
            AISPlainTextView *plainTextView = new AISPlainTextView;
            QApplication::setOverrideCursor(Qt::WaitCursor);
            plainTextView->setPlainText(dataSource->readFile());
            QApplication::restoreOverrideCursor();
            curDocView = plainTextView;
            break;
        }
        case DT_WORD:
        case DT_PPT:
        case DT_PDF:
        {
            AISPageDocView *pageDocView = new AISPageDocView;
            curDocView = pageDocView;
            break;
        }
        case DT_EXCEL:
        {
            break;
        }
        default:
            break;
        }

        if (curDocView)
        {
            QGridLayout *layoutMag = new QGridLayout;
            layoutMag->addWidget(curDocView);
            layoutMag->setContentsMargins(QMargins());
            setLayout(layoutMag);
            return true;
        }
    }

    return false;
}

QString AISDocView::getCurrentFilePath()
{
    return curFilePath;
}

AISDocView::DocumentType AISDocView::getDocumentType()
{
    QString ext = QFileInfo(curFilePath).suffix();
    if (0 == QString::compare(ext, "txt", Qt::CaseInsensitive))
    {
        return DT_TXT;
    }
    else if (0 == QString::compare(ext, "doc", Qt::CaseInsensitive) ||
             0 == QString::compare(ext, "docx", Qt::CaseInsensitive))
    {
        return DT_WORD;
    }
    else if (0 == QString::compare(ext, "ppt", Qt::CaseInsensitive) ||
             0 == QString::compare(ext, "pptx", Qt::CaseInsensitive))
    {
        return DT_PPT;
    }
    else if (0 == QString::compare(ext, "xls", Qt::CaseInsensitive) ||
             0 == QString::compare(ext, "xlsx", Qt::CaseInsensitive))
    {
        return DT_EXCEL;
    }
    else if (0 == QString::compare(ext, "pdf", Qt::CaseInsensitive))
    {
        return DT_PDF;
    }

    return DT_UNKNOWN;
}

QString AISDocView::getContentText()
{
    switch (getDocumentType())
    {
    case DT_TXT:
    {
        AISPlainTextView *plainTextView = (AISPlainTextView *)curDocView;
        return plainTextView->toPlainText();
    }
    default:
        return QString();
    }
}
